package com.beep.erp.model.earlystage.scan;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beep.erp.R;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.model.earlystage.standard.StandardActivity;
import com.beep.erp.model.out.OutOrderDetailActivity;
import com.beep.erp.model.stock.StockContract;
import com.beep.erp.model.stock.StockModel;
import com.beep.erp.model.stock.StockPresenter;
import com.beep.erp.model.transfer.pallet.PalletGoodsActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ScanActivity extends ToolbarActivity<StockPresenter, StockModel> implements StockContract.View {
    private static final String STATUS = "status";
    private static final String ISOUT = "isOut";
    @BindView(R.id.tv_scan)
    TextView tvScan;
    @BindView(R.id.tv_num_into)
    TextView tvNumInto;
    @BindView(R.id.rl_num_into)
    RelativeLayout rlNumInto;
    @BindView(R.id.line1)
    View line;


    private boolean isOut = false;
    private String status;

    public static void start(Context context, String status, boolean isOut) {
        Intent intent = new Intent(context, ScanActivity.class);
        intent.putExtra(STATUS, status);
        intent.putExtra(ISOUT, isOut);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_scan;
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected void initView() {
        getTvTitle().setText("扫码");
        isOut = getIntent().getBooleanExtra(ISOUT, false);
        status = getIntent().getStringExtra(STATUS);
        //status==3 代表 酱料期入
        rlNumInto.setVisibility(!isOut && status.equals("3") ? View.VISIBLE : View.GONE);
        line.setVisibility(!isOut && status.equals("3") ? View.VISIBLE : View.GONE);
    }

    private void doScan() {
        IntentIntegrator integrator = new IntentIntegrator(this);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.setPrompt("扫描条形码");
        integrator.setCameraId(0);
        integrator.setOrientationLocked(false);
        integrator.setBeepEnabled(true);//扫描提示音
        integrator.initiateScan();
    }

    @OnClick({R.id.tv_scan, R.id.tv_num_into})
    public void onClick(View view) {
        switch (view.getId()) {
            //酱料扫码期入 typeInfo=31
            case R.id.tv_scan:
                doScan();
                break;
            //酱料编码期入 typeInfo=32
            case R.id.tv_num_into:
                StandardActivity.start(this, "32");
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if (result != null) {
            if (result.getContents() == null) {
                ToastUtils.showShort("扫码取消！");
            } else {
                if (!isOut) {
                    //期出期入 扫码 31
                    if (status.equals("1")) {
                        PalletGoodsActivity.start(this, result.getContents(), "1");
                    } else if (status.equals("3")) {
                        PalletGoodsActivity.start(this, result.getContents(), "31");
                    }
                } else {
                    //出库 的扫码结果
                    OutOrderDetailActivity.start(this, result.getContents());
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

}
